package com.bdqn.controller;

import com.bdqn.entity.User;
import com.bdqn.mapper.Usermapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class controller {
    @Autowired
    Usermapper usermapper;

    @GetMapping("/findAll/{pageindex}/{pagesize}")
    public Page<User> findAll(@PathVariable(name = "pageindex") Integer pageindex , @PathVariable(name = "pagesize") Integer pagesize){
        Pageable pageable = PageRequest.of(pageindex-1,pagesize);
        return  usermapper.findAll(pageable);
    }

    @GetMapping("/getUser")
    User getUserById(Integer id){
        return  usermapper.findById(id).get();
    }

    @PostMapping("/save")
    String save(@RequestBody User user){
        User save = usermapper.save(user);
        if (save != null){
            return "succend";
        }else {
            return "lose";
        }
    }
    @PostMapping("/add")
    String add(@RequestBody User user){
        System.err.println(user);
        User save = usermapper.save(user);
        if (save != null){
            return "succend";
        }else {
            return "lose";
        }
    }

    @GetMapping("/delete/{id}")
    void delete(@PathVariable Integer id){
        usermapper.deleteById(id);
    }

}
